package com.smarttiger.zero.logic.impl;

import cn.hutool.db.DbUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.simple.SimpleDataSource;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;
import com.smarttiger.zero.admin.entity.ArgsModel;
import com.smarttiger.zero.admin.entity.DataSourceModel;
import com.smarttiger.zero.admin.util.Constant;
import com.smarttiger.zero.logic.BaseLogic;
import com.smarttiger.zero.logic.InterfaceContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhouzhengwei on 2021/10/25.
 */
@Component
public class SqlLogic extends BaseLogic {

    @Override
    public void logic() {
        HashMap<String,Object> test = new HashMap();
        List<ArgsModel> argsModels = getArgsModels();
        Map paramValue = (Map)getParamValue(Constant.INTERFACECURRENT);
        String sql = "";
        List<String> params = new ArrayList<>();
        for (ArgsModel map : argsModels) {
            if(map.getName().equals("sql")){
                sql = map.getDefaultValue();
            }
            if(!map.getName().equals("sql")){
                if(!StringUtils.isEmpty(map.getDefaultValue())){
                    params.add(map.getDefaultValue());
                }else{
                    String param = (String) paramValue.get(map.getName());
                    params.add(param);
                }
            }
        }
        if(sql.length()==0){
            Map<String, String> controll = InterfaceContext.logicControlContext.get();
            if(controll == null){
                controll = new HashMap<>();
            }
            controll.put(InterfaceContext.CONTROLL_ERROR,"sql参数没有设置");
            return;
        }
        Connection conn = null;
        try {
            DataSourceModel dataSourceModel = getDataSourceModel();
            DataSource dataSource = null;
            if(StringUtils.isEmpty(dataSourceModel.getDriver())){
                dataSource = new SimpleDataSource(
                        dataSourceModel.getUrl(),
                        dataSourceModel.getUserName(),
                        dataSourceModel.getPass());
            }else{
                dataSource = new SimpleDataSource(
                        dataSourceModel.getUrl(),
                        dataSourceModel.getUserName(),
                        dataSourceModel.getPass(),
                        dataSourceModel.getDriver());
            }

            conn = dataSource.getConnection();
            List<Entity> entityList = SqlExecutor.query(conn, sql, new EntityListHandler(),params.toArray(new String[params.size()]));
            test.put("data",entityList);
            setCurrentValue("data",entityList);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DbUtil.close(conn);
        }
        setResult(test);
    }
}
